#![allow(missing_docs, unused_variables, trivial_casts)]


#[allow(unused_imports)]
use futures::{future, Stream, stream};
#[allow(unused_imports)]
use local_api_rust::{Api, ApiNoContext, Claims, Client, ContextWrapperExt, models,
                      HelloGetResponse,
                      ProjectGetResponse,
                      ShowGetResponse,
                      ProjectProjectIdBugAllGetResponse,
                      ProjectProjectIdBugMyGetResponse,
                      ProjectProjectIdBugRecordBugIdEventsGetResponse,
                      ProjectProjectIdBugRecordBugIdShortNoteGetResponse,
                      ProjectProjectIdBugRecordBugIdShowGetResponse,
                      ProjectProjectIdCodeCommentCommentThreadIdGetResponse,
                      ProjectProjectIdCodeCommentCommentThreadIdOptionsResponse,
                      ProjectProjectIdCodeCommentCommentThreadIdPutResponse,
                      ProjectProjectIdCodeCommentCommentThreadIdCommentIdDeleteResponse,
                      ProjectProjectIdCodeCommentCommentThreadIdCommentIdGetResponse,
                      ProjectProjectIdCodeCommentCommentThreadIdCommentIdOptionsResponse,
                      ProjectProjectIdCodeCommentCommentThreadIdCommentIdPostResponse,
                      ProjectProjectIdEntryEntryTypeGetResponse,
                      ProjectProjectIdEntryShowEntryIdGetResponse,
                      ProjectProjectIdEventGetResponse,
                      ProjectProjectIdEventOptionsResponse,
                      ProjectProjectIdEventPostResponse,
                      ProjectProjectIdTaskAllGetResponse,
                      ProjectProjectIdTaskMyGetResponse,
                      ProjectProjectIdTaskRecordTaskIdDependGetResponse,
                      ProjectProjectIdTaskRecordTaskIdEventsGetResponse,
                      ProjectProjectIdTaskRecordTaskIdShortNoteGetResponse,
                      ProjectProjectIdTaskRecordTaskIdShowGetResponse,
                      ProjectProjectIdTaskRecordTaskIdSubTaskGetResponse,
                     };
use clap::{App, Arg};

// NOTE: Set environment variable RUST_LOG to the name of the executable (or "cargo run") to activate console logging for all loglevels.
//     See https://docs.rs/env_logger/latest/env_logger/  for more details

#[allow(unused_imports)]
use log::info;

// swagger::Has may be unused if there are no examples
#[allow(unused_imports)]
use swagger::{AuthData, ContextBuilder, EmptyContext, Has, Push, XSpanIdString};

type ClientContext = swagger::make_context_ty!(ContextBuilder, EmptyContext, Option<AuthData>, XSpanIdString);

mod client_auth;
use client_auth::build_token;


// rt may be unused if there are no examples
#[allow(unused_mut)]
fn main() {
    env_logger::init();

    let matches = App::new("client")
        .arg(Arg::with_name("operation")
            .help("Sets the operation to run")
            .possible_values(&[
                "HelloGet",
                "ProjectGet",
                "ShowGet",
                "ProjectProjectIdBugAllGet",
                "ProjectProjectIdBugRecordBugIdEventsGet",
                "ProjectProjectIdBugRecordBugIdShortNoteGet",
                "ProjectProjectIdBugRecordBugIdShowGet",
                "ProjectProjectIdCodeCommentCommentThreadIdGet",
                "ProjectProjectIdCodeCommentCommentThreadIdOptions",
                "ProjectProjectIdCodeCommentCommentThreadIdPut",
                "ProjectProjectIdCodeCommentCommentThreadIdCommentIdDelete",
                "ProjectProjectIdCodeCommentCommentThreadIdCommentIdGet",
                "ProjectProjectIdCodeCommentCommentThreadIdCommentIdOptions",
                "ProjectProjectIdCodeCommentCommentThreadIdCommentIdPost",
                "ProjectProjectIdEntryShowEntryIdGet",
                "ProjectProjectIdEventGet",
                "ProjectProjectIdEventOptions",
                "ProjectProjectIdEventPost",
                "ProjectProjectIdTaskAllGet",
                "ProjectProjectIdTaskRecordTaskIdDependGet",
                "ProjectProjectIdTaskRecordTaskIdEventsGet",
                "ProjectProjectIdTaskRecordTaskIdShortNoteGet",
                "ProjectProjectIdTaskRecordTaskIdShowGet",
                "ProjectProjectIdTaskRecordTaskIdSubTaskGet",
            ])
            .required(true)
            .index(1))
        .arg(Arg::with_name("https")
            .long("https")
            .help("Whether to use HTTPS or not"))
        .arg(Arg::with_name("host")
            .long("host")
            .takes_value(true)
            .default_value("localhost")
            .help("Hostname to contact"))
        .arg(Arg::with_name("port")
            .long("port")
            .takes_value(true)
            .default_value("8080")
            .help("Port to contact"))
        .get_matches();

    // Create Bearer-token with a fixed key (secret) for test purposes.
    // In a real (production) system this Bearer token should be obtained via an external Identity/Authentication-server
    // Ensure that you set the correct algorithm and encodingkey that matches what is used on the server side.
    // See https://github.com/Keats/jsonwebtoken for more information
    let auth_token = build_token(
            Claims {
                sub: "tester@acme.com".to_owned(),
                company: "ACME".to_owned(),
                iss: "my_identity_provider".to_owned(),
                // added a very long expiry time
                aud: "org.acme.Resource_Server".to_string(),
                exp: 10000000000,
                // In this example code all available Scopes are added, so the current Bearer Token gets fully authorization.
                scopes:
                  "".to_owned()
            },
            b"secret").unwrap();

    let auth_data = if !auth_token.is_empty() {
        Some(AuthData::Bearer(swagger::auth::Bearer { token: auth_token}))
    } else {
        // No Bearer-token available, so return None
        None
    };

    let is_https = matches.is_present("https");
    let base_url = format!("{}://{}:{}",
        if is_https { "https" } else { "http" },
        matches.value_of("host").unwrap(),
        matches.value_of("port").unwrap());

    let context: ClientContext =
        swagger::make_context!(ContextBuilder, EmptyContext, auth_data, XSpanIdString::default());

    let mut client : Box<dyn ApiNoContext<ClientContext>> = if matches.is_present("https") {
        // Using Simple HTTPS
        let client = Box::new(Client::try_new_https(&base_url)
            .expect("Failed to create HTTPS client"));
        Box::new(client.with_context(context))
    } else {
        // Using HTTP
        let client = Box::new(Client::try_new_http(
            &base_url)
            .expect("Failed to create HTTP client"));
        Box::new(client.with_context(context))
    };

    let mut rt = tokio::runtime::Runtime::new().unwrap();

    match matches.value_of("operation") {
        Some("HelloGet") => {
            let result = rt.block_on(client.hello_get(
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        Some("ProjectGet") => {
            let result = rt.block_on(client.project_get(
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        Some("ShowGet") => {
            let result = rt.block_on(client.show_get(
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        Some("ProjectProjectIdBugAllGet") => {
            let result = rt.block_on(client.project_project_id_bug_all_get(
                  "project_id_example".to_string(),
                  56,
                  56,
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        /* Disabled because there's no example.
        Some("ProjectProjectIdBugMyGet") => {
            let result = rt.block_on(client.project_project_id_bug_my_get(
                  "project_id_example".to_string(),
                  "access_token_example".to_string(),
                  ???
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        */
        Some("ProjectProjectIdBugRecordBugIdEventsGet") => {
            let result = rt.block_on(client.project_project_id_bug_record_bug_id_events_get(
                  "project_id_example".to_string(),
                  "bug_id_example".to_string(),
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        Some("ProjectProjectIdBugRecordBugIdShortNoteGet") => {
            let result = rt.block_on(client.project_project_id_bug_record_bug_id_short_note_get(
                  "project_id_example".to_string(),
                  "bug_id_example".to_string(),
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        Some("ProjectProjectIdBugRecordBugIdShowGet") => {
            let result = rt.block_on(client.project_project_id_bug_record_bug_id_show_get(
                  "project_id_example".to_string(),
                  "bug_id_example".to_string(),
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        Some("ProjectProjectIdCodeCommentCommentThreadIdGet") => {
            let result = rt.block_on(client.project_project_id_code_comment_comment_thread_id_get(
                  "project_id_example".to_string(),
                  "comment_thread_id_example".to_string(),
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        Some("ProjectProjectIdCodeCommentCommentThreadIdOptions") => {
            let result = rt.block_on(client.project_project_id_code_comment_comment_thread_id_options(
                  "project_id_example".to_string(),
                  "comment_thread_id_example".to_string(),
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        Some("ProjectProjectIdCodeCommentCommentThreadIdPut") => {
            let result = rt.block_on(client.project_project_id_code_comment_comment_thread_id_put(
                  "project_id_example".to_string(),
                  "comment_thread_id_example".to_string(),
                  "access_token_example".to_string(),
                  None
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        Some("ProjectProjectIdCodeCommentCommentThreadIdCommentIdDelete") => {
            let result = rt.block_on(client.project_project_id_code_comment_comment_thread_id_comment_id_delete(
                  "project_id_example".to_string(),
                  "comment_thread_id_example".to_string(),
                  "comment_id_example".to_string(),
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        Some("ProjectProjectIdCodeCommentCommentThreadIdCommentIdGet") => {
            let result = rt.block_on(client.project_project_id_code_comment_comment_thread_id_comment_id_get(
                  "project_id_example".to_string(),
                  "comment_thread_id_example".to_string(),
                  "comment_id_example".to_string(),
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        Some("ProjectProjectIdCodeCommentCommentThreadIdCommentIdOptions") => {
            let result = rt.block_on(client.project_project_id_code_comment_comment_thread_id_comment_id_options(
                  "project_id_example".to_string(),
                  "comment_thread_id_example".to_string(),
                  "comment_id_example".to_string(),
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        Some("ProjectProjectIdCodeCommentCommentThreadIdCommentIdPost") => {
            let result = rt.block_on(client.project_project_id_code_comment_comment_thread_id_comment_id_post(
                  "project_id_example".to_string(),
                  "comment_thread_id_example".to_string(),
                  "comment_id_example".to_string(),
                  "access_token_example".to_string(),
                  None
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        /* Disabled because there's no example.
        Some("ProjectProjectIdEntryEntryTypeGet") => {
            let result = rt.block_on(client.project_project_id_entry_entry_type_get(
                  "project_id_example".to_string(),
                  ???,
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        */
        Some("ProjectProjectIdEntryShowEntryIdGet") => {
            let result = rt.block_on(client.project_project_id_entry_show_entry_id_get(
                  "project_id_example".to_string(),
                  "entry_id_example".to_string(),
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        Some("ProjectProjectIdEventGet") => {
            let result = rt.block_on(client.project_project_id_event_get(
                  "project_id_example".to_string(),
                  789,
                  789,
                  56,
                  56,
                  "access_token_example".to_string(),
                  Some("user_id_example".to_string())
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        Some("ProjectProjectIdEventOptions") => {
            let result = rt.block_on(client.project_project_id_event_options(
                  "project_id_example".to_string(),
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        Some("ProjectProjectIdEventPost") => {
            let result = rt.block_on(client.project_project_id_event_post(
                  "project_id_example".to_string(),
                  "access_token_example".to_string(),
                  None
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        Some("ProjectProjectIdTaskAllGet") => {
            let result = rt.block_on(client.project_project_id_task_all_get(
                  "project_id_example".to_string(),
                  56,
                  56,
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        /* Disabled because there's no example.
        Some("ProjectProjectIdTaskMyGet") => {
            let result = rt.block_on(client.project_project_id_task_my_get(
                  "project_id_example".to_string(),
                  ???,
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        */
        Some("ProjectProjectIdTaskRecordTaskIdDependGet") => {
            let result = rt.block_on(client.project_project_id_task_record_task_id_depend_get(
                  "project_id_example".to_string(),
                  "task_id_example".to_string(),
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        Some("ProjectProjectIdTaskRecordTaskIdEventsGet") => {
            let result = rt.block_on(client.project_project_id_task_record_task_id_events_get(
                  "project_id_example".to_string(),
                  "task_id_example".to_string(),
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        Some("ProjectProjectIdTaskRecordTaskIdShortNoteGet") => {
            let result = rt.block_on(client.project_project_id_task_record_task_id_short_note_get(
                  "project_id_example".to_string(),
                  "task_id_example".to_string(),
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        Some("ProjectProjectIdTaskRecordTaskIdShowGet") => {
            let result = rt.block_on(client.project_project_id_task_record_task_id_show_get(
                  "project_id_example".to_string(),
                  "task_id_example".to_string(),
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        Some("ProjectProjectIdTaskRecordTaskIdSubTaskGet") => {
            let result = rt.block_on(client.project_project_id_task_record_task_id_sub_task_get(
                  "project_id_example".to_string(),
                  "task_id_example".to_string(),
                  "access_token_example".to_string()
            ));
            info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
        },
        _ => {
            panic!("Invalid operation provided")
        }
    }
}
